package com.yt.user.api.client;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.user.models.vo.CarouselInfoPicListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.user.models.service.ICarouselInfoService;
import com.yt.user.models.bo.CarouselInfoBO;
import com.yt.user.models.dto.CarouselInfoDTO;

import java.util.List;

/**
 * 轮播图控制器
 *
 * @author lvzeming
 * @Date 2025-08-14 16:44:08
 */

@RestController
@RequestMapping("/api/carouselInfo")
@Slf4j
@Api(value = "CarouselInfoApiController",tags = "轮播图Api接口")
public class CarouselInfoApiController {

    @DubboReference
    private ICarouselInfoService carouselInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<CarouselInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        CarouselInfoDTO dto = carouselInfoService.selectById(id);

        return new Result<>(dto);
    }


    @PostMapping("/getPics")
    @ApiOperation(value = "查询轮播图",notes = "查询轮播图")
    public Result<CarouselInfoPicListVO> getPics(){
        Wrapper<CarouselInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", 1);
        CarouselInfoDTO dto = carouselInfoService.selectOne(wrapper);
        CarouselInfoPicListVO carouselInfoPicListVO = new CarouselInfoPicListVO();
        if(null != dto){
            //将json转换成list
            carouselInfoPicListVO.setId(dto.getId());
            carouselInfoPicListVO.setName(dto.getName());
            carouselInfoPicListVO.setStatus(dto.getStatus());
            //1. 解析 JSON 字符串为 JSONObject
            JSONObject jsonObject = JSONObject.parseObject(dto.getPics());
            //2. 提取 "carouselpics" 对应的数组，并转换为 List<String>
            List<String> picList = JSONObject.parseArray(jsonObject.getString("carouselpics"), String.class);
            carouselInfoPicListVO.setPicList(picList);
        }

        return new Result<>(carouselInfoPicListVO);
    }

    @ApiOperation(value = "轮播图列表",notes = "轮播图列表")
    @PostMapping("/queryPageList")
    public Result<Page<CarouselInfoDTO>> queryPageList(CarouselInfoBO bo){
        Wrapper<CarouselInfoDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<CarouselInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = carouselInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
